package cn.iocoder.yudao.module.security.controller.admin.warnlog;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.security.controller.admin.warnlog.vo.*;
import cn.iocoder.yudao.module.security.dal.dataobject.warnlog.SecurityWarnLogDO;
import cn.iocoder.yudao.module.security.service.warnlog.SecurityWarnLogService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 告警日志")
@RestController
@RequestMapping("/security/warn-log")
@Validated
public class SecurityWarnLogController {

    @Resource
    private SecurityWarnLogService warnLogService;

    @PostMapping("/create")
    @Operation(summary = "创建告警日志")
    @PreAuthorize("@ss.hasPermission('security:warn-log:create')")
    public CommonResult<Long> createWarnLog(@Valid @RequestBody SecurityWarnLogSaveReqVO createReqVO) {
        return success(warnLogService.createWarnLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新告警日志")
    @PreAuthorize("@ss.hasPermission('security:warn-log:update')")
    public CommonResult<Boolean> updateWarnLog(@Valid @RequestBody SecurityWarnLogSaveReqVO updateReqVO) {
        warnLogService.updateWarnLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除告警日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('security:warn-log:delete')")
    public CommonResult<Boolean> deleteWarnLog(@RequestParam("id") Long id) {
        warnLogService.deleteWarnLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得告警日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('security:warn-log:query')")
    public CommonResult<SecurityWarnLogRespVO> getWarnLog(@RequestParam("id") Long id) {
        SecurityWarnLogDO warnLog = warnLogService.getWarnLog(id);
        return success(BeanUtils.toBean(warnLog, SecurityWarnLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得告警日志分页")
    @PreAuthorize("@ss.hasPermission('security:warn-log:query')")
    public CommonResult<PageResult<SecurityWarnLogRespVO>> getWarnLogPage(@Valid SecurityWarnLogPageReqVO pageReqVO) {
        PageResult<SecurityWarnLogDO> pageResult = warnLogService.getWarnLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SecurityWarnLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出告警日志 Excel")
    @PreAuthorize("@ss.hasPermission('security:warn-log:export')")
    @OperateLog(type = EXPORT)
    public void exportWarnLogExcel(@Valid SecurityWarnLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SecurityWarnLogDO> list = warnLogService.getWarnLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "告警日志.xls", "数据", SecurityWarnLogRespVO.class,
                        BeanUtils.toBean(list, SecurityWarnLogRespVO.class));
    }

}